import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { useStorage } from '@/hooks/useStorage'
import { User } from '@/types/user'

const storage = useStorage()
export const USER_KEY = 'user'

const userSlice = createSlice({
  name: 'user',
  initialState: storage.getStorage(USER_KEY) || ({} as User),
  reducers: {
    setInfo (_, action: PayloadAction<Partial<User>>) {
      storage.setStorage(USER_KEY, action.payload)

      return action.payload
    }
  }
})

export const { setInfo } = userSlice.actions

export default userSlice.reducer
